package com.ymy.thinking.in.spring.dependency.injection.resolution;

import com.ymy.thinking.in.spring.dependency.injection.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.DependencyDescriptor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.Arrays;

/**
 * 注解驱动的依赖注入(DI)处理过程
 *
 * @author Ringo
 * @date 2021/9/30 16:15
 * @see DependencyDescriptor
 */
public class AnnotationDependencyInjectionResolutionDemo {

    /**
     * 依赖查找（处理）
     * <br/>
     * DependencyDescriptor:
     * <li>必须 (require=true)</li>
     * <li>实施注入 (eager=true)</li>
     * <li>通过类型 (User.class)</li>
     * <li>字段名称（"user"）</li>
     */
    @Autowired
    private User user;

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext =
                new AnnotationConfigApplicationContext();
        applicationContext.register(AnnotationDependencyInjectionResolutionDemo.class);
        applicationContext.refresh();

        AnnotationDependencyInjectionResolutionDemo demo =
                applicationContext.getBean(AnnotationDependencyInjectionResolutionDemo.class);
        System.out.println("demo.user = " + demo.user);
    }

    @Bean
    public User user() {
        return new User("zsf", 108);
    }

}
